package cn.itcast.controller;

import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 文件导入
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/area_batchImport")
    public String upload(MultipartFile file) throws IOException {
        areaService.upload(file);
        return "redirect:/pages/base/area.html";
    }

    /**
     * 分页条件
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/area_pageQuery")
    @ResponseBody
    public HashMap<String, Object> findAll(Area area, int page, int rows){

        System.out.println("page:"+page);
        System.out.println("rows:"+rows);
        Page<Area> areas=null;
        if (area.getDistrict()==null&&area.getCity()==null&&area.getProvince()==null){
            areas = areaService.pageQuery(page, rows);
        }else {
            areas = areaService.pageQuery(area, page, rows);
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("total",areas.getTotalElements());
        result.put("rows",areas.getContent());

        return result;
    }
}
